package com.newcapec.servlet;

import javax.imageio.ImageIO;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

/**
 * @Description:生成验证码图片的servlet
 * @Author:yangshupeng
 * @Date: 2022/3/30 14:42
 * @Version:1.0
 */
public class ValidateCodeServlet extends HttpServlet {
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        //获取用户在xml配置的参数
        String width = config.getInitParameter("width");
        String height = config.getInitParameter("height");
        String nums = config.getInitParameter("nums");
        String codeStr = config.getInitParameter("codeStr");

        if (width != null){
            IMG_WIDTH = Integer.parseInt(width);
        }
        if (height != null){
            IMG_HEIGHT = Integer.parseInt(height);
        }
        if (nums != null){
            IMG_NUM = Integer.parseInt(nums);
        }
        if (codeStr != null){
            this.codeStr = codeStr;
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    //图片的高度,宽度,文本的个数
    private static  int IMG_HEIGHT = 30;
    private static  int IMG_WIDTH = 100;
    private static  int IMG_NUM = 4;
    private static String codeStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.设置响应头,图片的缓存
        resp.setHeader("Pragma", "no-cache");
        resp.setHeader("Cache-Control", "no-cache");
        //设置响应的内容类型,MIME TYPE
        resp.setContentType("image/jpeg");
        //创建绘制图片的缓存流,BufferedImage.TYPE_INT_RGB图片为三原色图片
        BufferedImage bi = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, BufferedImage.TYPE_INT_RGB);
        //获取画笔对象
        Graphics g = bi.getGraphics();
        //设置画笔的颜色
        g.setColor(new Color(100,230,200));
        //绘制图片的背景色
        g.fillRect(0, 0, IMG_WIDTH, IMG_HEIGHT);
        //验证码中使用字符
        char[] chars = codeStr.toCharArray();
        //创建随机数
        Random random = new Random();
        //创建StringBuffer存储随机字符
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < IMG_NUM; i++) {
            //生成随机数
            int index = random.nextInt(chars.length);
            //取出随机字符
            char randomChar = chars[index];
            //设置画笔颜色
            g.setColor(new Color(random.nextInt(150),random.nextInt(200),random.nextInt(255)));
            //设置文本字体
            g.setFont(new Font("Arial", Font.PLAIN,18));
            //将字符绘制到图片上
            g.drawString(String.valueOf(randomChar), 15+(i*20), 20);
            //将随机字符放入StringBuffer中
            buffer.append(randomChar);
        }
        //将存储随机字符的String对象放入Session中
        req.getSession().setAttribute("validateCode", buffer.toString());
        //通过ImageIo流，将图片输出到页面中
        ImageIO.write(bi, "jpg", resp.getOutputStream());
    }
}
